<?PHP
class Menu{
  static function display($id, $globalstart,$globalend, $elementstart,$elementend,$class){
    echo self::getCode($id, $globalstart,$globalend, $elementstart,$elementend,$class);
  }
  
  static function getCode($id, $globalstart,$globalend, $elementstart,$elementend,$class){
    if(Cache::contains("menu",$id."|".$globalstart."|".$globalstart."|".$elementstart."|".$elementend."|".$class)){
      $res = Cache::getData("menu",$id."|".$globalstart."|".$globalstart."|".$elementstart."|".$elementend."|".$class);
    }
    else{
      $res     = $globalstart;
      $entries = self::getEntries($id);
      $i = 1;
      if($entries){
        foreach($entries as $entry){ 
          $res .= $entry->getCode($globalstart,$globalend, $elementstart,$elementend,$class,$i);
          $i++;
        }
      }
      $res .= $globalend;
      Cache::setData("menu",$id."|".$globalstart."|".$globalstart."|".$elementstart."|".$elementend."|".$class,$res);
    }
    return $res;
  }
  
  function getIdByName($name){
    $name = $GLOBALS['db']->EscapeString(strtolower(trim($name)));
    return $GLOBALS['db']->ReadField("SELECT id FROM {'dbprefix'}menu_names WHERE lower(trim(name)) = '".$name."' LIMIT 0,1");;
  }

  function getEditableCode($id){
    $res = "";
    $entries = self::getEntries($id);
    if($entries){
      $actions = ActionList::get("menuedit");
      foreach($entries as $entry){ 
        $res .= $entry->getEditableCode($actions);
      }
    }
    return $res;
  }

  function displayEditable($id){
    echo $this->getEdiableCode($id);
  }

  function addEntry($menu,$title,$href){
    $maxID = $GLOBALS['db']->ReadField("SELECT MAX(id) FROM {'dbprefix'}menu 
                                        WHERE menuID = '".$menu."'");
    $id = $maxID + 1;
    $res = $GLOBALS['db']->Execute("INSERT INTO {'dbprefix'}menu (id,menuID,title,href)
                                    VALUES('".$id."','".$menu."',
                                    '".$title."','".$href."')");
    if($res){
      $args['menu']  = $menu;
      $args['title'] = $title;
      $args['href']  = $href;
      EventManager::raiseEvent("menu_entry_added","../",$args);
    }
    Cache::clear("menu");
    return $res;
  }

  function editEntry($menu,$id,$title,$href){
    $res =  $GLOBALS['db']->Execute("UPDATE {'dbprefix'}menu SET 
                                     href = '".$href."', 
                                     title = '".$title."' 
                                     WHERE id = '".$id."' 
                                     AND menuID = '".$menu."'");
    if($res){
      $args['menu']  = $menu;
      $args['title'] = $title;
      $args['href']  = $href;
      $args['id']    = $href;
      EventManager::raiseEvent("menu_entry_edit","../",$args);
    }
    Cache::clear("menu");
    return $res;
  }

  function deleteEntry($menu,$id){
    $res = $GLOBALS['db']->Execute("DELETE FROM {'dbprefix'}menu WHERE menuID = '".$menu."' AND id = '".$id."'");
    if($res){
      $res = $GLOBALS['db']->Execute("UPDATE {'dbprefix'}menu SET id = id - 1 WHERE menuID = '".$menu."'  AND id > ".$id."");
      if($res){
        $args['menu']  = $menu;
        $args['id'] = $id;
        EventManager::raiseEvent("menu_entry_deleted","../",$args);
      }
    }
    Cache::clear("menu");
    return $res;
  }

  function create($name,$dir = ""){
    $name = $GLOBALS['db']->EscapeString($name);
    $dir = $GLOBALS['db']->EscapeString($dir);
    $res = $GLOBALS['db']->Execute("INSERT INTO {'dbprefix'}menu_names (name, dir) VALUES ('".$name."', '".$dir."')");
    if($res){
      $args['name']  = $name;
      EventManager::raiseEvent("menu_created","../",$args);
    }
    Cache::clear("menu");
    return $GLOBALS['db']->InsertID();
  }

  function delete($id){
    $res = $GLOBALS['db']->Execute("DELETE FROM {'dbprefix'}menu_names WHERE id = '".$id."'");
    if($res){
      $res = $GLOBALS['db']->Execute("DELETE FROM {'dbprefix'}menu WHERE menuID = '".$id."'");
      if($res){
        $args['id']  = $id;
        EventManager::raiseEvent("menu_deleted","../",$args);
      }
    }
    Cache::clear("menu");
    return $res;
  }
  
  static function getEntries($id){
    $res = null;
    $rows = $GLOBALS['db']->ReadRows("SELECT * FROM {'dbprefix'}menu WHERE menuID = '".$id."' ORDER BY id");
    if($rows){
      foreach($rows as $row){
        $entry = new MenuEntry();
        $entry->id    = $row->id;
        $entry->type  = $row->type;
        $entry->href  = $row->href;
        $entry->title = $row->title;
        $entry->menu  = $id;
        $res[] = $entry;
      }
    }
    return $res;
  }

  public static function countEntries($menu){
    $menu  = $GLOBALS['db']->EscapeString($menu);
    return $GLOBALS['db']->ReadField("SELECT COUNT(*) FROM {'dbprefix'}menu WHERE menuID = '".$menu."'");
  }

  public static function changePositions($menu, $item1, $item2){
    $entries = self::countEntries($menu);
    if($item1 > 0 && $item2 > 0 && $item1 < $entries + 1 && $item2 < $entries + 1){
      $item1 = $GLOBALS['db']->EscapeString($item1);
      $item2 = $GLOBALS['db']->EscapeString($item2);
      $menu  = $GLOBALS['db']->EscapeString($menu);
      $GLOBALS['db']->Execute("UPDATE {'dbprefix'}menu SET id = -1 WHERE menuID = '".$menu."'  AND id = ".$item1);
      $GLOBALS['db']->Execute("UPDATE {'dbprefix'}menu SET id = ".$item1." WHERE menuID = '".$menu."'  AND id = ".$item2);
      $GLOBALS['db']->Execute("UPDATE {'dbprefix'}menu SET id = ".$item2." WHERE menuID = '".$menu."'  AND id = -1");
      Cache::clear("menu");
    }
  }

}
?>